package com.geqian.web.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author geqian
 * @date 15:15 2022/11/1
 */
public class StringCommonUtils {

    /**
     * 下划线转小驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToSmallHump(String str) {
        Pattern pattern = Pattern.compile("_[a-z0-9]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(0).replace("_", "").toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 下划线转大驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToBigHump(String str) {
        String[] words = str.split("_");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(words[0].toLowerCase());
        for (int i = 1; i < words.length; i++) {
            stringBuilder.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1));
        }
        return stringBuilder.substring(0, 1).toUpperCase() + stringBuilder.substring(1);
    }

    /**
     * 小驼峰转下划线
     *
     * @param str
     * @return
     */
    public static String smallHumpToUnderline(String str) {
        Pattern pattern = Pattern.compile("[A-Z]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 大驼峰峰转下划线
     *
     * @param str
     * @return
     */
    public static String bigHumpToUnderline(String str) {
        Pattern pattern = Pattern.compile("[A-Z]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString().substring(1);
    }


    /**
     * 大驼峰峰转小驼峰
     *
     * @param str
     * @return
     */
    public static String bigHumpToSmallHump(String str) {
        return str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toLowerCase(Locale.ROOT));
    }

    /**
     * 小驼峰转大驼峰
     *
     * @param str
     * @return
     */
    public static String smallHumpToBigHump(String str) {
        return str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase(Locale.ROOT));
    }



    /**
     * 解析正则匹配字符串集合
     *
     * @param str        字符串
     * @param regex      匹配正则表达式
     * @param groupIndex 分组下标
     * @return
     */
    public static List<String> findMatchingStrings(String str, String regex, int groupIndex) {
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        List<String> matchingStrings = new ArrayList<>();
        while (matcher.find()) {
            matchingStrings.add(matcher.group(groupIndex));
        }
        return matchingStrings;
    }


    /**
     * 截取正则匹配的子字符串
     *
     * @param str        str
     * @param regex      匹配正则表达式
     * @param groupIndex 分组下标
     * @return
     */
    public static String findMatchingString(String str, String regex, int groupIndex) {
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        List<String> subStringList = new ArrayList<>();
        while (matcher.find()) {
            subStringList.add(matcher.group(groupIndex));
        }
        switch (subStringList.size()) {
            case 1:
                return subStringList.get(0);
            case 0:
                throw new RuntimeException("没有找到匹配的字符串");
            default:
                throw new RuntimeException("找到" + subStringList.size() + "个匹配的字符串");
        }
    }


    /**
     * 解析匹配字符串并替换字符串
     *
     * @param str        字符串
     * @param regex      匹配正则表达式
     * @param groupIndex 分组下标
     * @param replaceStr 替换字符串
     * @return
     */
    public static String findAndReplace(String str, String regex, int groupIndex, String replaceStr) {
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        while (matcher.find()) {
            str = str.replace(matcher.group(groupIndex), replaceStr);
        }
        return str;
    }


    /**
     * 解析匹配字符串并替换字符串
     *
     * @param str        字符串
     * @param regex      匹配正则表达式
     * @param groupIndex 分组下标
     * @param character  替换字符
     * @return
     */
    public static String findAndReplace(String str, String regex, int groupIndex, Character character) {
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(str);
        while (matcher.find()) {
            str = str.replace(matcher.group(groupIndex), getReplaceChars(character, matcher.group(groupIndex).length()));
        }
        return str;
    }


    /**
     * 生成有某个字符组成，长度count的字符串
     *
     * @param character
     * @param count
     * @return
     */
    private static String getReplaceChars(Character character, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(character);
        }
        return sb.toString();
    }

}
